package yana.tpe.shop.domain.domains;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class AccountBook implements Serializable {

    private List<Product> products = new ArrayList<Product>();
    private List<Seller> sellers = new ArrayList<Seller>();
    private List<Trade> trades = new ArrayList<Trade>();

    private transient Map<Class<?>, List<? extends AbstractDomain>> map;

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public List<Seller> getSellers() {
        return sellers;
    }

    public void setSellers(List<Seller> sellers) {
        this.sellers = sellers;
    }

    public List<Trade> getTrades() {
        return trades;
    }

    public void setTrades(List<Trade> trades) {
        this.trades = trades;
    }

    public <T extends AbstractDomain> List getList(Class<T> clazz) {
        init();
        return map.get(clazz);
    }

    public <T extends AbstractDomain> T getEntyty(Class<T> clazz, Long id) {
        init();
        List<T> list = getList(clazz);
        if (list != null && !list.isEmpty()) {
            for(T t : list) {
                if (id.equals(t.getId())) {
                    return t;
                }
            }
        }
        return null;
    }

    private void init(){
        if (map == null) {
            map = new HashMap<Class<?>, List<? extends AbstractDomain>>();

            map.put(Product.class, products);
            map.put(Seller.class, sellers);
            map.put(Trade.class, trades);
        }
    }
}
